#region File Description
//-----------------------------------------------------------------------------
// PhoneMainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using System;
using GameStateManagement;
using Microsoft.Xna.Framework;
using System.IO;
using System.IO.IsolatedStorage;

namespace GameStateManagementSample
{
    class PhoneMainMenuScreen : PhoneMenuScreen
    {
        public IsolatedStorageSettings gameSettings = IsolatedStorageSettings.ApplicationSettings;
        public PhoneMainMenuScreen()
            : base("Battle of Fourway")
        {
            setupSetting();
            // Create a button to start the game
            Button playButton = new Button("Play");
            playButton.Tapped += playButton_Tapped;
            MenuButtons.Add(playButton);

           
            // Create two buttons to toggle sound effects and music. This sample just shows one way
            // of making and using these buttons; it doesn't actually have sound effects or music
            BooleanButton sfxButton = new BooleanButton("Sound Effects", ScreenManager.soundEnabled);
            sfxButton.Tapped += sfxButton_Tapped;
            MenuButtons.Add(sfxButton);

            BooleanButton musicButton = new BooleanButton("Music", ScreenManager.haveMusic);
            musicButton.Tapped += musicButton_Tapped;
            MenuButtons.Add(musicButton);

            BooleanButton vibrationButton = new BooleanButton("Vibration", ScreenManager.VibrationEnabled);
            vibrationButton.Tapped += vibrationButton_Tapped;
            MenuButtons.Add(vibrationButton);

            Button instructionsButton = new Button("Instructions");
            instructionsButton.Tapped += instructionsButton_Tapped;
            MenuButtons.Add(instructionsButton);

            Button creditsButton = new Button("Credits");
            creditsButton.Tapped += creditsButton_Tapped;
            MenuButtons.Add(creditsButton);
        }
        void setupSetting()
        {
            if (gameSettings.Contains("Music"))
            {
                ScreenManager.haveMusic = (bool)gameSettings["Music"];
            }
            else
            {
                gameSettings.Add("Music", ScreenManager.haveMusic);
            }

            if (gameSettings.Contains("Sound Effects"))
            {
                ScreenManager.soundEnabled = (bool)gameSettings["Sound Effects"];
            }
            else
            {
                gameSettings.Add("Sound Effects", ScreenManager.soundEnabled);
            }

            if (gameSettings.Contains("Vibration"))
            {
                ScreenManager.VibrationEnabled = (bool)gameSettings["Vibration"];
            }
            else
            {
                gameSettings.Add("Vibration", ScreenManager.VibrationEnabled);
            }
            
        }
        void updateSetting()
        {
            gameSettings["Music"] = ScreenManager.haveMusic;
            gameSettings["Sound Effects"] = ScreenManager.soundEnabled;
            gameSettings["Vibration"] = ScreenManager.VibrationEnabled;
        }
        void playButton_Tapped(object sender, EventArgs e)
        {
            // When the "Play" button is tapped, we load the GameplayScreen
            LoadingScreen.Load(ScreenManager, true, PlayerIndex.One, new GameplayScreen());

            ScreenManager.Vibrate(50);
        }

        void sfxButton_Tapped(object sender, EventArgs e)
        {
            BooleanButton button = sender as BooleanButton;

            ScreenManager.soundEnabled = !ScreenManager.soundEnabled;
            updateSetting();
            ScreenManager.Vibrate(50);
        }

        void musicButton_Tapped(object sender, EventArgs e)
        {
           
            BooleanButton button = sender as BooleanButton;

            ScreenManager.haveMusic = !ScreenManager.haveMusic;

            if (ScreenManager.haveMusic && ScreenManager.musicManager != null)
            {
                ScreenManager.musicManager.Play(ScreenManager.gameMusic);
            }
            else
            {
                ScreenManager.musicManager.Stop();
            }
            updateSetting();
            ScreenManager.Vibrate(50);
        }

        void vibrationButton_Tapped(object sender, EventArgs e)
        {
            ScreenManager.VibrationEnabled = !ScreenManager.VibrationEnabled;
            ScreenManager.Vibrate(50);
            updateSetting();
            BooleanButton button = sender as BooleanButton;
            
        }

        void instructionsButton_Tapped(object sender, EventArgs e)
        {
            ScreenManager.Vibrate(50);
            LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
                                                           new instructionsScreen());
        }

        void creditsButton_Tapped(object sender, EventArgs e)
        {
            ScreenManager.Vibrate(50);
            LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
                                                           new CreditsScreen());
        }

        protected override void OnCancel()
        {
            ScreenManager.Game.Exit();
            base.OnCancel();
        }

        
    }
}
